package com.interview.ujiuye2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Copyright (C), 2018-2020
 * FileName: ArrayListDemo
 * Author:   kongfanyu
 * Date:     2020/10/12 14:17
 */
public class ArrayListDemo {
    public static void main(String[] args) {
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 30; i++) {
            new Thread(
                    new Runnable() {
                        @Override
                        public void run() {
                            String substring = UUID.randomUUID().toString().substring(0, 8);
                            list.add(substring);
                            System.out.println(list);
                        }
                    }
            ).start();
        }
    }

    private static void test2() {
        //ArrayList<String> list = new ArrayList<>();
        List<String> list = Collections.synchronizedList(new ArrayList<>());
        System.out.println(list.getClass().getName());
        for (int i = 0; i < 30; i++) {

            new Thread(
                    new Runnable() {
                        @Override
                        public void run() {
                            String substring = UUID.randomUUID().toString().substring(0, 8);
                            list.add(substring);
                            System.out.println(list);
                        }
                    }
            ).start();


        }
    }

    private static void test1() {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");
        list.add("G");
        list.add("H");
        list.add("I");
        list.add("J");
        list.add("K");
        System.out.println(list.size());
    }
}
